package com.naizhuai.util.command;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.naizhuai.ui.entity.Bus.Bus;
import com.naizhuai.util.AppUtils;
import com.naizhuai.util.ConstantsParams;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.RxBus;

import java.util.ArrayList;
import java.util.List;

public class RemindReceiver extends BroadcastReceiver {
    public static final String ACTION_START = "COM.NAIZHU.ACTION_START";
    public static final String ACTION_REMIND = "COM.NAIZHU.ACTION_REMIND";
    public static final String ACTION_UPDATE = "COM.NAIZHU.ACTION_UPDATE";
    public static final String ACTION_STOP = "COM.NAIZHU.ACTION_STOP";

    private Context mContext;

//    private static AlarmManager mAlarmManager;

//    private static boolean mScheduleReminderFlag = false;

    @Override
    public void onReceive(Context ctx, Intent intent) {
        mContext = ctx.getApplicationContext();
        String action = intent.getAction();
        NaizhuAILog.d("ReminderReceiver action=" + action + "mContext=" + mContext);
        if (action == null)
            return;
        switch (action) {
//            case "android.intent.action.USER_PRESENT":
////                scheduleReminderStart(mContext);
//                startService();
//                break;
//            case Intent.ACTION_BATTERY_CHANGED:
////                scheduleReminderStart(mContext);
//                startService();
//                break;
            case ACTION_START:
//                scheduleReminderStart(mContext);
                startService();
                break;
            case ACTION_REMIND:
                startService();
                break;
            case ACTION_STOP:
//                cancelReminder(ctx);
                break;
            case ACTION_UPDATE:
//                cancelReminder(ctx);
                RxBus.getDefault().postSticky(new Bus.UserChange());
                startService();
                break;
        }
    }

    private void startService() {
        CommandsService.startInitService(mContext, null);
    }

//    public static void scheduleReminderStart(Context context) {
//        if (!mScheduleReminderFlag) {
//            scheduleReminder(context);
//            mScheduleReminderFlag = true;
//            NaizhuAILog.d("scheduleReminderStart ...");
//        } else {
//            NaizhuAILog.d("scheduleReminderStart alread start...");
//        }
//    }

//    public static void scheduleReminder(Context context) {
//        long triggerTime, intervaTime = ConstantsParams.getAskServerTriggerTimer();
////		UtilLog.d("ReminderReceiver:---->scheduleReminder ");
//        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//        Intent reminderIntent = new Intent(context, RemindReceiver.class);
//        reminderIntent.setAction(ACTION_REMIND);
//        reminderPendingIntent = PendingIntent.getBroadcast(context, 0, reminderIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//        triggerTime = System.currentTimeMillis() + ConstantsParams.getAskServerTriggerTimer();
//        mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, intervaTime, reminderPendingIntent);
//    }


//    //取消广播
//    public static void cancelReminder(Context context) {
//        if (reminderPendingIntent != null) {
//            AlarmManager mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//            mAlarmManager.cancel(reminderPendingIntent);
//            reminderPendingIntent.cancel();
//            reminderPendingIntent = null;
//            mScheduleReminderFlag = false;
//            NaizhuAILog.v("ReminderReceiver: cancelReminder()");
//        }
//    }

    //开启广播
    public static void startReceiver(Context mContext, Intent intent) {
        if (intent == null) {
            intent = new Intent();
        }
        intent.setAction(ACTION_START);
        mContext.sendBroadcast(intent);
    }

    //保留广播
    public static void remindReceiver(Context mContext, Intent intent) {
        if (intent == null) {
            intent = new Intent();
        }
        intent.setAction(ACTION_REMIND);
        mContext.sendBroadcast(intent);
    }

    //更新广播
    public static void updateReceiver(Context mContext, Intent intent) {
        if (intent == null) {
            intent = new Intent();
        }
        intent.setAction(ACTION_UPDATE);
        mContext.sendBroadcast(intent);
    }

}
